package com.shxzz.easy12306.services.pay.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.shxzz.easy12306.framework.starter.designpattern.strategy.AbstractStrategyChoose;
import com.shxzz.easy12306.services.pay.common.enums.PayChannelEnum;
import com.shxzz.easy12306.services.pay.dto.ali.AliPayCallbackRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 支付回调控制层
 * @author shxzz
 * @create 2024-03-11-22:18
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class PayCallbackController {

    private final AbstractStrategyChoose abstractStrategyChoose;

    /**
     * 支付宝回调
     * 调用支付宝支付后，支付宝会调用此接口发送支付结果
     */
    @PostMapping("/api/pay-service/callback/alipay")
    public void callbackAlipay(@RequestParam Map<String, Object> requestParam) {
        Object refundFee = requestParam.get("refund_fee");
        if(refundFee != null) {
            log.info("收到退款回调，退款金额：{}", refundFee);
            return;
        }
        AliPayCallbackRequest aliPayCallbackRequest = BeanUtil.mapToBean(requestParam, AliPayCallbackRequest.class, true, CopyOptions.create());
        aliPayCallbackRequest.setChannel(String.valueOf(PayChannelEnum.ALI_PAY.getCode()));

        abstractStrategyChoose.chooseAndExecute(aliPayCallbackRequest.buildMark(), aliPayCallbackRequest);
    }
}
